//
//  MHComicCommentViewController.m
//  ManHua
//
//  Created by 江子安 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHComicCommentViewController.h"
#import "MHComicCommentTableViewCell.h"
#import <AFNetworking.h>
#import "MHReplyListVC.h"
#import "MHComicCommentViewController.h"


#define kCommentURL @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx"



@interface MHComicCommentViewController ()<UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) UITextField *inputTF;// 评论输入框
@property (nonatomic, strong) UIButton *commentBtn;
@property (nonatomic, strong) UIView *inputView;

@end

@implementation MHComicCommentViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (NSMutableArray *)dataSourceArr
{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
        
    }
    return _dataSourceArr;
}



- (void)requestDataWithMethod:(NSString *)method
{
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithCapacity:0];

    dict1[@"uuid"] = @"537e76184314b8c8beb37f75bacafb14edbe0d97";
    dict1[@"method"] = method; // 最新的时commentlist   "hotcommentlist"
    dict1[@"bookid"] = self.bookid;

   
    
    [[AFHTTPRequestOperationManager manager] GET:kCommentURL parameters:dict1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"===========%@",responseObject);
        NSArray *arr = responseObject;
        for (NSDictionary *dict in arr) {
            MHComicComentModel *model = [[MHComicComentModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArr addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
   
    
}


// 添加一个输入框
- (void)addCommentInputView
{
    _inputView = [[UIView alloc]initWithFrame:CGRectMake(0, Height - 40, Width, 40)];
    _inputView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_inputView];
    
    _inputTF = [[UITextField alloc]initWithFrame:CGRectMake(15, 5, Width - 30 - 60 - 15, 30)];
    _inputTF.delegate = self;
    _inputTF.borderStyle = UITextBorderStyleRoundedRect;
    _inputTF.placeholder = @"写评论...";
    [_inputView addSubview:_inputTF];
    
    _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commentBtn.frame = CGRectMake(_inputTF.frame.size.width + 30, 5, 60, 30);
    _commentBtn.backgroundColor = [UIColor colorWithRed:0.246 green:0.922 blue:0.204 alpha:1.000];
    [_commentBtn setTitle:@"评论" forState:UIControlStateNormal];
    [_inputView addSubview:_commentBtn];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    // 评论需要请求两个数据。
    [self requestDataWithMethod:@"hotcommentlist"];
    [self requestDataWithMethod:@"commentlist"];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView registerClass:[MHComicCommentTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    [self addCommentInputView];
    
    
    // 给键盘添加监控
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

#pragma mark - 文本框代理
/**
 *  点击了return按钮(键盘最右下角的按钮)就会调用
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    // 返回YES即可
    return YES;
}
/**
 *  当键盘改变了frame(位置和尺寸)的时候调用
 */
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    
    
    // 0.取出键盘动画的时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    // 1.取得键盘最后的frame
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
    
    // 3.执行动画
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
}
/**
 *  当开始拖拽表格的时候就会调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 退出键盘
    [self.view endEditing:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicComentModel *model = self.dataSourceArr[indexPath.row];
    
    
    return [MHComicCommentTableViewCell heightOfCellWithModdel:model];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MHComicComentModel *model = self.dataSourceArr[indexPath.row];
    [cell configureDataWithModel:model];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicComentModel *model = self.dataSourceArr[indexPath.row];
    
    MHReplyListVC *replyListVC = [[MHReplyListVC alloc]init];
    
    replyListVC.commentid = [NSString stringWithFormat:@"%@",model.CommentID];
    
    [self.navigationController pushViewController:replyListVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
